package com.qk.manangement.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.response.PageResult;
import com.qk.entity.pojo.Course;
import com.qk.manangement.mapper.CourseMapper;
import com.qk.manangement.service.CourseService;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;


    @Override
    public PageResult<Course> list(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Course> courseList = courseMapper.list(name, subject, target);
        PageInfo<Course> coursePageInfo = new PageInfo<>(courseList);
        PageResult<Course> coursePageResult = new PageResult<>(coursePageInfo.getTotal(), coursePageInfo.getList());
        return coursePageResult;
    }


    public PageResult<Course> listByMP(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        // 参数校验
        if (page == null || pageSize == null || page <= 0 || pageSize <= 0) {
            return new PageResult<>(0L, List.of());
        }

        // 创建分页对象
        Page<Course> coursePage = new Page<>(page, pageSize);

        // 创建查询条件
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name)
                .eq(subject != null, "subject", subject)
                .eq(target != null, "target", target);

        // 执行分页查询
        Page<Course> resultPage = courseMapper.selectPage(coursePage, queryWrapper);

        // 封装返回结果
        return new PageResult<>(resultPage.getTotal(), resultPage.getRecords());
    }


    @Override
    public boolean deleteCourse(Integer id) {
        // 参数校验
        if (id == null) {
            return false;
        }

        // 执行删除操作并返回结果
        return courseMapper.deleteById(id) > 0;
    }


    @Override
    public boolean addCourse(Course course) {
        // 参数校验
        if (course == null) {
            return false;
        }

        // 校验必要字段
        if (course.getSubject() == null ||
                course.getName() == null ||
                course.getPrice() == null ||
                course.getTarget() == null) {
            return false;
        }

        // 设置创建和更新时间
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());

        // 插入课程信息
        return courseMapper.insert(course) > 0;
    }


    @Override
    public Course getById(Integer id) {
        // 参数校验
        if (id == null) {
            return null;
        }

        // 查询并返回结果
        return courseMapper.getById(id);
    }


    @Override
    public boolean updateCourse(Course course) {
        // 参数校验
        if (course == null || course.getId() == null) {
            return false;
        }

        // 查询原有课程信息
        Course existingCourse = getById(course.getId());
        if (existingCourse == null) {
            return false;
        }

        // 更新非空字段
        if (course.getName() != null) {
            existingCourse.setName(course.getName());
        }
        if (course.getSubject() != null) {
            existingCourse.setSubject(course.getSubject());
        }
        if (course.getPrice() != null) {
            existingCourse.setPrice(course.getPrice());
        }
        if (course.getTarget() != null) {
            existingCourse.setTarget(course.getTarget());
        }
        if (course.getDescription() != null) {
            existingCourse.setDescription(course.getDescription());
        }

        // 设置更新时间
        existingCourse.setUpdateTime(LocalDateTime.now());

        // 执行更新操作
        return courseMapper.updateById(existingCourse) > 0;
    }


    @Override
    public List<Course> list() {
        return courseMapper.list(null, null, null);
    }


    @Override
    public List<Course> listBySubject(Integer subject) {
        return courseMapper.list(null, subject, null);
    }

}




